#ifndef VIDEOMANAGERSINGLETON_H
#define VIDEOMANAGERSINGLETON_H

#include <mlt++/Mlt.h>
#include <QThread>
#include <QString>
#include <QList>
#include <QTimer>
#include <iostream>
#include <QFile>
#include <QTextStream>
#include "ExtractionManager.h"
#include "SubtitleParser.h"

#define N_MAX_SUBTITLE_TRACKS 4


class VideoManagerSingleton : public QThread{
    Q_OBJECT
    public:
    static VideoManagerSingleton *getInstance();
    ~VideoManagerSingleton();
    void run();
    void initMlt(int mainVideoWidgetId, int extractedVideoWidgetId);
    void openVideo(QString fileName);
    void restoreAfterCrash();
    void _restoreExtractions(QTextStream &in);
    void createRestoreFile();
    void addExtractionInRestoreFile(Extraction extraction);
    int getNFrames();
    double getFps();
    int getCurrentTimeMainVideo();
    int getCurrentPositionMainVideo();
    int getMaxTimeMainVideo();
    int getCurrentTimeExtractedVideo();
    int getCurrentPositionExtractedVideo();
    int getMaxTimeExtractedVideo();
    void addExtractionWithSimpleMethod(int nSecBefore, int nSecAfter);
    void addExtractionWithAutomaticMethod(int nMaxSecBefore, int nMaxSecAfter, double sensibility, int medianDataSize);
    Extraction getExtraction(int id);
    Extraction getLastExtraction();
    int getNExtractions();
    void removeExtraction(int id);
    void removeAllExtractions();
    void selectExtraction(int id);
    void replaceExtraction(int indice, Extraction newExtraction);
    void playMainVideo();
    bool isPlayingMainVideo();
    bool isPlayingExtractedVideo();
    bool isStopMainVideo();
    void pauseMainVideo();
    void pauseConsumerMainVideo();
    void stopMainVideo();
    void seekMainVideo(int idFrame);
    void playExtractedVideo();
    void pauseExtractedVideo();
    void pauseConsumerExtractedVideo();
    void stopExtractedVideo();
    void seekExtractedVideo(int idFrame);
    void setVolume(int percentage);
    void saveExtractions(QString fileName, bool audioOnly = false);
    //new
    void setSubtitleTrack(int idSubtitleTrack, QString strFileName, QFont font, QColor color, double yPerc);
    void removeSubtitleTrack(int idSubtitleTrack);
    //new
    bool isCalculingExtraction();
    bool isCalculingWaveform();

    //TODO add methods for subtitles track management. They will probably take a bounch of parameters such as font, color, positions...

    signals:

    void newExtractionAdded(int start, int end);
    void newVideoOpened();
    void videoFinished();
    void videoSaved();
    void calculingExtraction();
    //void waveFormGot();

    public slots:
    void registerNewExtraction(int start, int end);
    //new
    void eventuallyDeleteSubtitleParser();
    void checkIfMainVideoIsStillPlaying();
    //new
    void emitCalculingExtraction();
    //void emitWaveFormGot();


    //void run();

    private:
    VideoManagerSingleton();
    static VideoManagerSingleton *instance;
    bool isMainVideoPlaying;
    bool isExtractedVideoPlaying;
    bool isMainVideoStopped;
    float *waveForm;
    //new
    Mlt::Tractor *mainTractor; //replace mainVideoProducer
    Mlt::Multitrack *mainTractorMultitrack;
    Mlt::Producer *baseColorProducer;
    Mlt::Transition *firstCompositionTransition;
    Mlt::Transition *mixTransition;
    //Mlt::Playlist *subtitleTracks[N_MAX_SUBTITLE_TRACKS];
    //SubtitleParser *subtitleParsers[N_MAX_SUBTITLE_TRACKS];
    //Mlt::Transition *subtitleTransitions[N_MAX_SUBTITLE_TRACKS];
    QList<SubtitleParser*> *subtitleParserToDelete;
    void _initTractor(Mlt::Producer *mainVideoProducer);
    void _deleteTractorAndPlaylists();
    void _deletePlaylist(Mlt::Playlist *playlist);
    void _deleteSubtitleParser(SubtitleParser *subtitleParser);
    QTimer *updateTimer;
    //new

    ExtractionManager extractionManager;
    QString videoFileName;
    QString outVideoFileName;
    bool audioOnly;
    Mlt::Profile *readingVideoProfile;
    Mlt::Producer *mainVideoProducer;
    Mlt::Producer *extractedVideoProducer;
    Mlt::Consumer *mainVideoConsumer;
    Mlt::Consumer *extractedVideoConsumer;
    Mlt::Filter *volumeFilter;
    Mlt::Properties *volumeProperties;
    void _removeRegionFromPlaylist(Mlt::Playlist &playlist);
    void _freeMemoryBeforeOpeningVideo();


    void _playMainVideoAfterStop();
    void _playMainVideoAfterExtract();
    void _playMainVideoAfterPause();
    void _extractWaveForm();




};


#endif // VIDEOMANAGERSINGLETON_H
